using System;
using System.Collections.Generic;
using System.Text;
using QLHXNK.DataAccess;
using System.Data;

namespace QLHXNK.BusinessLogic
{
    class Item
    {
        private int itemID;
        private String itemCode;
        private String itemName;
        private String itemSpecs;
        private String itemUnit;
        private String remark;

        #region Properties
        public int ID
        {
            get { return itemID; }
        }
        public String Code
        {
            get { return itemCode; }
            set { itemCode = value; }
        }
        public String Name
        {
            get { return itemName; }
            set { itemName = value; }
        }

        public String ItemSpecs
        {
            get { return itemSpecs; }
            set { itemSpecs = value; }
        }

        public String ItemUnit
        {
            get { return itemUnit; }
            set { itemUnit = value; }
        }

        public String Remark
        {
            get { return remark; }
            set { remark = value; }
        }

        

        #endregion
        public Item(int itemID, String itemCode, String itemName, String itemSpecs, String itemUnit, String remark)
        {
            this.itemID = itemID;
            this.itemCode = itemCode;
            this.itemName = itemName;
            this.itemSpecs = itemSpecs;
            this.itemUnit = itemUnit;
            this.remark = remark;
        }

        #region ----------------------------------------Lấy thông tin hàng----------------------------------------

        public static DataTable ListItem()
        {
            try
            {
                return ItemData.GetItem("");
            }
            catch 
            {
                return null;
            }
        }

        public static DataTable GetItemByName(String itemName)
        {
            try
            {
                return ItemData.GetItem(itemName);
            }
            catch
            {
                return null;
            }
        }

        public static DataTable GetItemInImportNote(int ID)
        {
            try
            {
                return ItemData.GetItemInImportNote(ID);
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region ------------------------------------------Thêm, xoá, sửa-------------------------------------------

        public static Item CreateItem(String itemCode, String itemName, String itemSpecs, String itemUnit, String remark)
        {
            try
            {
                int id = ItemData.CreateItem(itemCode, itemName, itemSpecs,itemUnit,remark);
                Item item = null;
                if (id != 0)
                    item = new Item(id, itemCode, itemName, itemSpecs,itemUnit,remark);
                return item;
            }
            catch 
            {
                return null;
            }
        }

        public int DeleteItem()
        {
            try
            {
                return ItemData.DeleteItem(this.itemID);
            }
            catch 
            {
                return 0;
            }
        }

        public int Save()
        {
            try
            {
                return ItemData.UpdateItem(this.itemID,this.itemCode,this.itemName,this.itemSpecs,this.itemUnit,this.remark);
            }
            catch 
            {
                return 0;
            }
        }

        public int UpdateItem(String itemCode, String itemName, String itemSpecs, String itemUnit, String remark)
        {
            try
            {
                this.itemCode = itemCode;
                this.itemName = itemName;
                this.itemSpecs = itemSpecs;
                this.itemUnit = itemUnit;
                this.remark = remark;
                return Save();
            }
            catch 
            {
                return 0;
            }
        }

        #endregion
    }
}
